package com.wydpp.utils;

import com.wydpp.config.SipDeviceConfig;
import com.wydpp.model.response.OrgnizationInfo;
import com.wydpp.model.response.BackTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.Map;

@Component
public class HttpUtil {

    @Autowired
    private SipDeviceConfig sipDeviceConfig;

    public  OrgnizationInfo getDeviceInfo() {
        String deviceUrl = sipDeviceConfig.getDeviceUrl();
        OrgnizationInfo reslut = null;
        try {
            WebClient webClient = WebClient.create(deviceUrl);
            Flux<OrgnizationInfo> fAns =
                    webClient.get().uri(deviceUrl)
                            .retrieve().bodyToFlux(OrgnizationInfo.class);
            reslut = fAns.blockLast();
        }catch (Exception e){
            return null;
        }
        return reslut;
    }

    public int pushRealTimeStream (String ip, int port,String clientId,int channelNo,
                           int mediaType,int streamType) {
        int reslut;
        // 使用map的方式，来映射参数
        Map<String, Object> uriVariables = new HashMap<>(6);
        uriVariables.put("p1", ip);
        uriVariables.put("p2", port);
        uriVariables.put("p3", clientId);
        uriVariables.put("p4", channelNo);
        uriVariables.put("p5", mediaType);
        uriVariables.put("p6", streamType);
        String realtimeUrl = sipDeviceConfig.getRealTimeUrl();
        try{
            WebClient webClient = WebClient.create(realtimeUrl);
            Flux<Integer> fAns =
                    webClient.get().uri(realtimeUrl+"?ip={p1}&port={p2}&clientId={p3}&channelNo={p4}&mediaType={p5}&streamType={p6}", uriVariables)
                            .retrieve().bodyToFlux(Integer.class);
            reslut = fAns.blockLast();
        }catch (Exception e){
            return 0;
        }
        return reslut;
    }
    public int pushBackPlayStream (String ip, int port,String clientId,int channelNo,
                                   int mediaType,int streamType,String startTime,String endTime) {
        int reslut;
        // 使用map的方式，来映射参数
        Map<String, Object> uriVariables = new HashMap<>(8);
        uriVariables.put("p1", ip);
        uriVariables.put("p2", port);
        uriVariables.put("p3", clientId);
        uriVariables.put("p4", channelNo);
        uriVariables.put("p5", mediaType);
        uriVariables.put("p6", streamType);
        uriVariables.put("p7", startTime);
        uriVariables.put("p8", endTime);
        String backPlayUrl = sipDeviceConfig.getBackPlayUrl();
        try{
            WebClient webClient = WebClient.create(backPlayUrl);
            Flux<Integer> fAns =
                    webClient.get().uri(backPlayUrl+"?ip={p1}&port={p2}&clientId={p3}&channelNo={p4}&mediaType={p5}&streamType={p6}&startTime={p7}&endTime={p8}", uriVariables)
                            .retrieve().bodyToFlux(Integer.class);
            reslut = fAns.blockLast();
        }catch (Exception e){
            return 0;
        }
        return reslut;
    }


    public BackTime getBackTime (String clientId, int channelNo,
                                 int mediaType, int streamType, int storageType, String startTime, String endTime) {
        BackTime reslut=null;
        // 使用map的方式，来映射参数
        Map<String, Object> uriVariables = new HashMap<>(8);
        uriVariables.put("p1", clientId);
        uriVariables.put("p2", channelNo);
        uriVariables.put("p3", mediaType);
        uriVariables.put("p4", streamType);
        uriVariables.put("p5", storageType);
        uriVariables.put("p6", startTime);
        uriVariables.put("p7", endTime);
        String backPlayUrl = sipDeviceConfig.getBackPlayTime();
        try{
            WebClient webClient = WebClient.create(backPlayUrl);
            Flux<BackTime> fAns =
                    webClient.get().uri(backPlayUrl+"?clientId={p1}&channelNo={p2}&mediaType={p3}&streamType={p4}&storageType={p5}&startTime={p6}&endTime={p7}", uriVariables)
                            .retrieve().bodyToFlux(BackTime.class);
            reslut = fAns.blockLast();
        }catch (Exception e){
            return null;
        }
        return reslut;
    }





    public int closeRealTimeStream (String clientId,int channelNo,
                                   int mediaType,int streamType) {
        int reslut;
        // 使用map的方式，来映射参数
        Map<String, Object> uriVariables = new HashMap<>(4);
        uriVariables.put("p1", clientId);
        uriVariables.put("p2", channelNo);
        uriVariables.put("p3", mediaType);
        uriVariables.put("p4", streamType);
        String realtimeClose = sipDeviceConfig.getRealtimeClose();
        try{
            WebClient webClient = WebClient.create(realtimeClose);
            Flux<Integer> fAns =
                    webClient.get().uri(realtimeClose+"?clientId={p1}&channelNo={p2}&mediaType={p3}&streamType={p4}", uriVariables)
                            .retrieve().bodyToFlux(Integer.class);
            reslut = fAns.blockLast();
        }catch (Exception e){
            return 0;
        }
        return reslut;
    }

    public int closeBackPlayStream (String clientId,int channelNo,
                                    int mediaType,int streamType,
                                    String startTime,String endTime) {
        int reslut;
        // 使用map的方式，来映射参数
        Map<String, Object> uriVariables = new HashMap<>(6);
        uriVariables.put("p1", clientId);
        uriVariables.put("p2", channelNo);
        uriVariables.put("p3", mediaType);
        uriVariables.put("p4", streamType);
        uriVariables.put("p5", startTime);
        uriVariables.put("p6", endTime);

        String backClose = sipDeviceConfig.getBackClose();
        try{
            WebClient webClient = WebClient.create(backClose);
            Flux<Integer> fAns =
                    webClient.get().uri(backClose+"?clientId={p1}&channelNo={p2}&mediaType={p3}&streamType={p4}&startTime={p5}&endTime={p6}", uriVariables)
                            .retrieve().bodyToFlux(Integer.class);
            reslut = fAns.blockLast();
        }catch (Exception e){
            return 0;
        }
        return reslut;
    }
}
